$(document).ready(function(){
    $("#batal").click(function(){
        window.location.href = base;
    });
    
    $("a#delete").bind("click", function(){
        var id= $(this).attr("value");
        $(".container").append("<div title='Hapus' id='hapus'>Anda Yakin akan Menghapus data Ini ?</div>");
        dialog(id);
        return false;
    });
    
    $("a#deleteAnggota").bind("click", function(){
        var id= $(this).attr("value");
        var id_project = $(".content").find("input#id_proyek").attr("value");
        $(this).parent().addClass('del');
        $("#project-frag-2").append("<div title='Hapus' id='hapus'>Anda Yakin akan Menghapus data Ini ?</div>");
        dialogAnggota(id, id_project);
        return false;
    });
    
    $("a#deleteGenset").bind("click", function(){
        var id= $(this).attr("value");
        $(this).parent().addClass('del');
        $("#project-frag-4").append("<div title='Hapus' id='hapus'>Anda Yakin akan Menghapus data Ini ?</div>");
        dialogGenset(id);
        return false;
    });

    $("tbody tr.view").bind("click", function(){
       var id = $(this).attr("id");
       window.location.href = base+'/view/id/'+id;
    });
    
   function dialogAnggota(id, id_project) {
   $( "#hapus" ).dialog({modal: true, buttons: {
                Yes: function() {
                    $('table#anggota').find('.del').parent().empty();
                    $.post('/aventurine_project/async/deletepegawai', {'id':id, 'id_project':id_project},'' ,'html');
                    $( this ).dialog( "close" );
                },
                No: function() {$(this).dialog( "close" );}}});
   }
   
   function dialogGenset(id) {
   $( "#hapus" ).dialog({modal: true, buttons: {
                Yes: function() {
                    $('table#tabelgenset').find('.del').parent().empty();
                    $.post('/aventurine_project/async/deletegenset', {'id':id},'' ,'html');
                    $( this ).dialog( "close" );
                },
                No: function() {$(this).dialog( "close" );}}});
   }
   
   function dialog(id) {
   $( "#hapus" ).dialog({modal: true, buttons: {
                Yes: function() {$.post(base+'/delete', {'id':id},'' ,'html');$( this ).dialog( "close" );window.location.href = base;},
                No: function() {$(this).dialog( "close" );}}});
   }
   
   $.ajaxSetup({cache:false});
   $("#thisContent").html("Loading...").load("/aventurine_project/laporangenset/calendar");
   $("#calendar").click(function(){
        $("#thisContent").html("Loading...").load("/aventurine_project/laporangenset/calendar");
        return false;
   });
   $("#proyek").click(function(){
        $("#thisContent").html("Loading...").load("/aventurine_project/laporangenset/proyek");
        return false;
   });
   $("#gensetButton").click(function(){
        $("#thisContent").html("Loading...").load("/aventurine_project/laporangenset/genset");
        return false;
   });
   
   $("input#jam_kerja").numeric({decimal: false, negative: false}, function() {this.value = "";this.focus();});
   $("input#jam_kerja").keyup(function(){
       var jam = $("input#jam_kerja").val();
       $("input#rencana_waktu_berlangsung").val(jam*20);
   });
});

function deleteGensetOnClick(id)
{
    var id_project = $(".content").find("input#id_proyek").attr("value");
    $("#project-frag-2").append("<div title='Hapus' id='hapus'>Anda Yakin akan Menghapus data Ini ?</div>");
    dialogGensetSession(id, id_project);
}

function deleteAnggotaOnClick(id)
{
    $("#project-frag-4").append("<div title='Hapus' id='hapus'>Anda Yakin akan Menghapus data Ini ?</div>");
    dialogAnggotaSession(id);
}

function dialogAnggotaSession(id, id_project) {
$( "#hapus" ).dialog({modal: true, buttons: {
            Yes: function() {
                $('table#anggota').find('#anggota_'+id).empty();
                $.post('/aventurine_project/async/deletepegawai', {'id':id, 'id_project':id_project},'' ,'html');
                $( this ).dialog( "close" );
            },
            No: function() {$(this).dialog( "close" );}}});
}

function dialogGensetSession(id) {
$( "#hapus" ).dialog({modal: true, buttons: {
            Yes: function() {
                $('table#tabelgenset').find('#genset_'+id).empty();
                $.post('/aventurine_project/async/deletegenset', {'id':id},'' ,'html');
                $( this ).dialog( "close" );
            },
            No: function() {$(this).dialog( "close" );}}});
}